/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

#include "main_ability_slice.h"
#include "ability_env.h"
#include "ability_manager.h"
#include "components/ui_label.h"
#include "components/ui_label_button.h"
#include "components/ui_image_view.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include<stdio.h>
#include "file.h"
#include "securec.h"

namespace OHOS
{
REGISTER_AS(MainAbilitySlice)

constexpr static int ROOT_VIEW_POSITION_X = 0;
constexpr static int ROOT_VIEW_POSITION_Y = 0;
constexpr static int ROOT_VIEW_WIDTH = 960;
constexpr static int ROOT_VIEW_HEIGHT = 480;
static constexpr uint16_t MAX_PATH_LENGTH = 512;
static const char* const BACK_ICON_PATH = "/displayFont/assets/entry/resources/base/media/ic_back.png";
static char g_backIconAbsolutePath[MAX_PATH_LENGTH] = {0};
void MainAbilitySlice::OnStart(const Want &want)
{
    printf("MainAbilitySlice::OnStart\n");
    AbilitySlice::OnStart(want);
    const char* pathHeader = GetSrcPath();
    if(sprintf_s(g_backIconAbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,BACK_ICON_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backIconAbsolutePath error");
        return;
    }
    backIcon = new UIImageView();
    backIcon->SetPosition(0, 0);
    backIcon->SetSrc(g_backIconAbsolutePath);
    backIcon->SetTouchable(true);
    backIcon->Resize(40,40);
    auto onClick = [this] (UIView& view, const Event& event) -> bool {
        TerminateAbility();
        return true;
    };
    backIconListener = new EventListener(onClick, nullptr);
    backIcon->SetOnClickListener(backIconListener);
    
    first_title = new UILabel();
    first_title->SetPosition(0,0,960,50);
    first_title->SetTextColor(Color::Green());
    first_title->SetText("1.系统自有字体-思源黑体：");
    first_title->SetFont("SourceHanSansSC-Regular.otf",35);

    first_content = new UILabel();
    first_content->SetPosition(0,50,960,150);
    first_content->SetText("每一位开发者都是华为要汇聚的星星之火！");
    first_content->SetFont("SourceHanSansSC-Regular.otf",120);
    first_content->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE);

    second_title = new UILabel();
    second_title->SetPosition(0,200,960,50);
    second_title->SetTextColor(Color::Green());
    second_title->SetText("2.沐瑶软笔手写体：");
    second_title->SetTextColor(Color::Green());
    second_title->SetFont("Muyao-Softbrush.ttf", 35);

    second_content = new UILabel();
    second_content->SetPosition(0,250,960,150);
    second_content->SetText("每一位开发者都是华为要汇聚的星星之火！");
    second_content->SetFont("Muyao-Softbrush.ttf", 120);
    second_content->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE);

    third_title = new UILabel();
    third_title->SetPosition(0,400,960,50);
    third_title->SetTextColor(Color::Green());
    third_title->SetText("3.包图小白体：");
    third_title->SetFont("baotuxiaobaiti.ttf", 35);

    third_content = new UILabel();
    third_content->SetPosition(0,450,960,150);
    third_content->SetText("每一位开发者都是华为要汇聚的星星之火！");
    third_content->SetFont("baotuxiaobaiti.ttf",120);
    third_content->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE);

    fourth_title = new UILabel();
    fourth_title->SetPosition(0,600,960,50);
    fourth_title->SetTextColor(Color::Green());
    fourth_title->SetText("4.王漢宗勘亭流繁：");
    fourth_title->SetFont("wangHangZong.ttf",35);

    fourth_content = new UILabel();
    fourth_content->SetPosition(0,650,960,150);
    fourth_content->SetText("每壹位開發者都是華為要匯聚的星星之火！");
    fourth_content->SetFont("wangHangZong.ttf",120);
    fourth_content->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE);

    scroll = new UIScrollView();
    scroll->SetPosition(0,40,960,440);
    scroll->SetYScrollBarVisible(true);
    scroll->SetXScrollBarVisible(true);

    scroll->Add(first_title);
    scroll->Add(first_content);
    scroll->Add(second_title);
    scroll->Add(second_content);
    scroll->Add(third_title);
    scroll->Add(third_content);
    scroll->Add(fourth_title);
    scroll->Add(fourth_content);

    rootView_ = RootView::GetWindowRootView();
    rootView_->SetPosition(ROOT_VIEW_POSITION_X, ROOT_VIEW_POSITION_Y,ROOT_VIEW_WIDTH, ROOT_VIEW_HEIGHT);
    rootView_->Add(backIcon);
    rootView_->Add(scroll);

    SetUIContent(rootView_);
}

void MainAbilitySlice::OnStop()
{
    printf("MainAbilitySlice::OnStop\n");
    AbilitySlice::OnStop();
    if (backIconListener != nullptr) {
        delete backIconListener;
        backIconListener = nullptr;
    }
    if (backIcon != nullptr) {
        delete backIcon;
        backIcon = nullptr;
    }
    if (first_title != nullptr) {
        delete first_title;
        first_title = nullptr;
    }
    if (first_content != nullptr) {
        delete first_content;
        first_content =nullptr;
    }
    if (second_title != nullptr) {
        delete second_title;
        second_title = nullptr;
    }
    if (second_content != nullptr) {
        delete second_content;
        second_content = nullptr;
    }
    if (third_title != nullptr) {
        delete third_title;
        third_title = nullptr;
    }
    if (third_content != nullptr){
        delete third_content;
        third_content = nullptr;
    }
    if (fourth_title != nullptr) {
        delete fourth_title;
        fourth_title = nullptr;
    }
    if (fourth_content != nullptr) {
        delete fourth_content;
        fourth_content = nullptr;
    }
    if (rootView_ != nullptr){
        RootView::DestoryWindowRootView(rootView_);
        rootView_=nullptr;
    }
}
} // namespace OHOS
